#include "include/transmssion/dmk_transmssion.h"
#include "include/non_standard_utility.h"
#include "include/paramnonstandard485.h"
#include <QDebug>

DMK_Transmssion::DMK_Transmssion( QObject* parent, ParamNonStandard485* param ) : TransmissionInter( parent, param ) {}

DMK_Transmssion::~DMK_Transmssion() {}

void DMK_Transmssion::init( QList< QByteArray >* writeList, int state_ID ) {
    //控制字模式：（06 00 05 00 00）：0：内部速度做指令速度
    QByteArray contro( 6, 0x00 );
    //控制_485: (06 00 25 00 00）:1: 485,0:io
    QByteArray contro_485( 6, 0x00 );
    //启动:(06 00 22 00 00） 0：停止，1：启动
    QByteArray start( 6, 0x00 );
    contro[ 0 ] = contro_485[ 0 ] = start[ 0 ] = static_cast< uchar >( state_ID );
    contro[ 1 ] = contro_485[ 1 ] = start[ 1 ] = 0x06;
    contro[ 3 ]                                = 0x05;
    contro_485[ 3 ]                            = 0x25;
    start[ 3 ]                                 = 0x22;
    contro_485[ 5 ]                            = 0x01;

    QByteArray contro_485_crc = Non_Standard_Utility::CRC16_ModBus( contro_485, contro_485.count() );
    contro_485.append( contro_485_crc );
    QByteArray contro_crc = Non_Standard_Utility::CRC16_ModBus( contro, contro.count() );
    contro.append( contro_crc );
    QByteArray start_crc = Non_Standard_Utility::CRC16_ModBus( start, start.count() );
    start.append( start_crc );
    writeList->append( contro );
    writeList->append( contro_485 );
    //开始停止
    writeList->append( start );
}

void DMK_Transmssion::onReadHandler( const QByteArray& readByte ) {
    static TransMissionState recoreAction = Start_Forward_ING;

    if ( readByte.size() == 7 ) {
        if ( readByte.at( 1 ) == 0x03 && readByte.at( 4 ) != 0x00 ) {
            qDebug() << "DMK_Transmssion ReadHandler" << readByte.toHex();
            //错误码
            TransMissionErrorCode core    = TransMissionErrorCode( readByte.at( 4 ) );
            uchar                 stateID = readByte.at( 0 );
            emit                  sigTransmissionState( getTransMissionData( stateID, TransMissionState::Error, core ) );
        }
    }
    if ( readByte.size() == 8 ) {
        uchar             stateID     = readByte.at( 0 );
        TransAction       transAction = Stop;
        TransMissionState state       = Stop_ING;
        if ( readByte.at( 1 ) == 0x06 ) {
            switch ( readByte.at( 3 ) ) {
            case 0x22:
                //启停
                if ( readByte.at( 5 ) == 0x01 ) { state = recoreAction; }
                emit sigTransmissionState( getTransMissionData( stateID, state, transAction ) );
                break;
            case 0x23:
                //正反
                if ( readByte.at( 5 ) == 0x00 ) { recoreAction = Start_Forward_ING; }
                if ( readByte.at( 5 ) == 0x01 ) { recoreAction = Start_Reverse_ING; }
                break;
            default:
                break;
            }
        }
    }
}

void DMK_Transmssion::onAddWriteData( QList< QByteArray >* writeList, const TransMissionData& transMissionData ) {
    switch ( static_cast< char >( transMissionData.trans_action ) ) {
    case TransAction::Start_Forward: {
        //正转（06 00 23 00 00）：0：正转、1：反转
        QByteArray direction( 6, 0x00 );
        //设置速度: (06 00 06 00 00）
        QByteArray speed( 6, 0x00 );
        //启动:(06 00 22 00 00） 0：停止，1：启动
        QByteArray start( 6, 0x00 );
        direction[ 0 ] = speed[ 0 ] = start[ 0 ] = static_cast< char >( transMissionData.device_address );
        direction[ 1 ]                           = 0x06;
        speed[ 1 ]                               = 0x06;
        start[ 1 ]                               = 0x06;
        direction[ 3 ]                           = 0x23;
        speed[ 3 ]                               = 0x06;
        start[ 3 ]                               = 0x22;
        speed[ 4 ]                               = static_cast< char >( param_p->getTransmissionConfig()->TransmissionSpeed >> 8 );
        speed[ 5 ]                               = static_cast< char >( param_p->getTransmissionConfig()->TransmissionSpeed );
        start[ 5 ]                               = 0x01;

        QByteArray direction_crc = Non_Standard_Utility::CRC16_ModBus( direction, direction.count() );
        direction.append( direction_crc );
        QByteArray speed_crc = Non_Standard_Utility::CRC16_ModBus( speed, speed.count() );
        speed.append( speed_crc );
        QByteArray start_crc = Non_Standard_Utility::CRC16_ModBus( start, start.count() );
        start.append( start_crc );

        writeList->append( direction );
        writeList->append( speed );
        writeList->append( start );
    } break;
    case TransAction::Start_Reverse: {
        //正转（06 00 23 00 00）：0：正转、1：反转
        QByteArray direction( 6, 0x00 );
        //设置速度: (06 00 06 00 00）
        QByteArray speed( 6, 0x00 );
        //启动:(06 00 22 00 00） 0：停止，1：启动
        QByteArray start( 6, 0x00 );
        direction[ 0 ] = speed[ 0 ] = start[ 0 ] = static_cast< char >( transMissionData.device_address );
        direction[ 1 ] = speed[ 1 ] = start[ 1 ] = 0x06;
        direction[ 3 ]                           = 0x23;
        direction[ 5 ]                           = 0x01;
        speed[ 3 ]                               = 0x06;
        start[ 3 ]                               = 0x22;
        speed[ 4 ]                               = static_cast< char >( param_p->getTransmissionConfig()->TransmissionSpeed >> 8 );
        speed[ 5 ]                               = static_cast< char >( param_p->getTransmissionConfig()->TransmissionSpeed );
        start[ 5 ]                               = 0x01;

        QByteArray direction_crc = Non_Standard_Utility::CRC16_ModBus( direction, direction.count() );
        direction.append( direction_crc );
        QByteArray speed_crc = Non_Standard_Utility::CRC16_ModBus( speed, speed.count() );
        speed.append( speed_crc );
        QByteArray start_crc = Non_Standard_Utility::CRC16_ModBus( start, start.count() );
        start.append( start_crc );

        writeList->append( direction );
        writeList->append( speed );
        writeList->append( start );
        break;
    }
    case TransAction::Stop: {
        //停止:(06 00 22 00 00） 0：停止，1：启动
        QByteArray start( 6, 0x00 );
        start[ 0 ]           = static_cast< char >( transMissionData.device_address );
        start[ 1 ]           = 0x06;
        start[ 3 ]           = 0x22;
        start[ 5 ]           = 0x00;
        QByteArray start_crc = Non_Standard_Utility::CRC16_ModBus( start, start.count() );
        start.append( start_crc );

        writeList->append( start );
        break;
    }
    //    case TransAction::Brak: {
    //        //刹车:(06 00 24 00 00） 0：松刹车，1：刹车
    //        QByteArray start( 6, 0x00 );
    //        start[ 0 ]           = static_cast< char >( transMissionData.device_address );
    //        start[ 1 ]           = 0x06;
    //        start[ 3 ]           = 0x24;
    //        start[ 5 ]           = 0x01;
    //        QByteArray start_crc = Non_Standard_Utility::CRC16_ModBus( start, start.count() );
    //        start.append( start_crc );
    //        writeList->append( start );
    //        break;
    //    }
    //    case NO_Brak: {
    //        //刹车:(06 00 24 00 00） 0：松刹车，1：刹车
    //        QByteArray start( 6, 0x00 );
    //        start[ 0 ]           = static_cast< char >( transMissionData.device_address );
    //        start[ 1 ]           = 0x06;
    //        start[ 3 ]           = 0x24;
    //        start[ 5 ]           = 0x00;
    //        QByteArray start_crc = Non_Standard_Utility::CRC16_ModBus( start, start.count() );
    //        start.append( start_crc );
    //        writeList->append( start );
    //    }
    default:
        break;
    }
}

void DMK_Transmssion::onAddReadData( QList< QByteArray >* readList, int state_ID ) {
    //读取错误码
    QByteArray readData( 6, 0x00 );
    readData[ 0 ]           = static_cast< char >( state_ID );
    readData[ 1 ]           = 0x03;
    readData[ 2 ]           = 0x01;
    readData[ 3 ]           = 0x11;
    readData[ 5 ]           = 0x01;
    QByteArray readData_crc = Non_Standard_Utility::CRC16_ModBus( readData, readData.count() );
    readData.append( readData_crc );
    readList->append( readData );
}
